package com.cnhis.cloudhealth.clinical.osworks.commonsql.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OperationHocusDao;
import com.cnhis.cloudhealth.clinical.osworks.commonsql.dao.OsHooperDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.Xml2JsonUtil;
import com.cnhis.cloudhealth.commons.vo.exception.BaseException;

@Service
public class OsHooperBo {
	
	@Resource
	private OsHooperDao osHooperDao;	

	@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	public ModelVo updatehOOperInfoEntry(Map<String, Object> map) throws Exception{
		Map<String, Object> mapxml = null;
		ModelVo mv = new ModelVo();
		try {
			mapxml = Xml2JsonUtil.xmlElements(map.get("Doc").toString().toLowerCase());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("解析XML文档时发生错误，错误号: %d.");
		}
			List tmpVAT1 = (List) mapxml.get("rxlist");
			if(tmpVAT1.size()<=0){
				//throw new BaseException("没有找到要提交的发药内容，请重新后重试！");
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("提交错误，病人手术信息为空！");
				return mv;
			}
			
			//list转换成map
			Map mapList = (Map) tmpVAT1.get(0);
			
			//*************************更新病人手术记录***********************/
			String BEE03="";
			String BEE01=GlobalVariable.StringIsNull(mapList.get("bee01"));
			if(BEE01!=""){
				BEE03= osHooperDao.getBEE03(mapList);
				mapList.remove("bee01");
				mapList.put("bee01", BEE01);
			}else{
				BEE03="";
				mapList.remove("bee01");
				mapList.put("bee01", 0);
			}
			mapList.put("bee03", BEE03);
			String date1=GlobalVariable.StringIsNull(mapList.get("vat09"));
			/*if(date1==""&&BEE01==""){
				int i1 = osHooperDao.updateVAT1isnullDateBee1(mapList);
				if(i1 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新病人手术信息时发生错误！");
					return mv;
				}
			}else if(date1==""){
				int i1 = osHooperDao.updateVAT1isnullDate(mapList);
				if(i1 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新病人手术信息时发生错误！");
					return mv;
				}
			}else if(BEE01==""){
				int i1 = osHooperDao.updateVAT1isnullBee1(mapList);
				if(i1 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新病人手术信息时发生错误！");
					return mv;
				}
			}else{
				mapList.remove("BEE01");
				mapList.put("BBE01", Integer.parseInt(BEE01));
				int i1 = osHooperDao.updateVAT1(mapList);
				if(i1 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新病人手术信息时发生错误！");
					return mv;
				}
			}*/
			int ii2 = osHooperDao.updateVAT1(mapList);
			if(ii2<=0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新病人手术信息出错,请检查后再次提交!");
				return mv;
			}
			
			//-更新手术间状态
			if(BEE01 != ""){
				int i2 = osHooperDao.updateBEE1(Integer.parseInt(BEE01));
				if(i2 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新病人手术信息时发生错误！");
					return mv;
				}
			}
			
			//**************************写入手术人员安排信息**********************/
			/*		  SELECT @NurseCode = MZOperCode FROM #tmpVAT1
					  select @Count = COUNT(1) from #tmpVAX1
					  select IDENTITY( INT,1,1 ) AS ID , BCE02,BCE03 into #tmpBCE11 from BCE1
					   where BCE02 in (select * from SPLIT(@NurseCode,'/'))
					  select @i = 0,@Count2 = count(*) from #tmpBCE11 
					  while @i<@Count
					  begin
					    insert into #tmpVAX1(ID , BCE02 , BCE03 , ACE01) 
					    select @Count + @i+1,BCE02, BCE03 , '11' FROM #tmpBCE11 WHERE ID = @i+1
					    SET @i = @I +1
					  end
					  IF OBJECT_ID('tempdb..#tmpBCE11') IS NOT NULL  DROP TABLE #tmpBCE11 
					  
					  SELECT @NurseCode = XsNurseCode FROM #tmpVAT1
					  select @Count = COUNT(1) from #tmpVAX1
					  select IDENTITY( INT,1,1 ) AS ID , BCE02,BCE03 into #tmpBCE from BCE1
					   where BCE02 in (select * from SPLIT(@NurseCode,'/'))
					  select @i = 0,@Count2 = count(*) from #tmpBCE 
					  while @i<@Count
					  begin
					    insert into #tmpVAX1(ID , BCE02 , BCE03 , ACE01) 
					    select @Count + @i+1,BCE02, BCE03 , '21' FROM #tmpBCE WHERE ID = @i+1
					    SET @i = @I +1
					  end
					  IF OBJECT_ID('tempdb..#tmpBCE') IS NOT NULL  DROP TABLE #tmpBCE 

					  SELECT @NurseCode = XhNurseCode FROM #tmpVAT1
					  select @Count = COUNT(1) from #tmpVAX1
					  select IDENTITY( INT,1,1 ) AS ID , BCE02,BCE03 into #tmpBCE2 from BCE1
					   where BCE02 in (select * from SPLIT(@NurseCode,'/'))
					  select @i = 0,@Count2 = count(*) from #tmpBCE2 
					  while @i<@Count
					  begin
					    insert into #tmpVAX1(ID , BCE02 , BCE03 , ACE01) 
					    select @Count + @i+1,BCE02, BCE03 , '22' FROM #tmpBCE2 WHERE ID = @i+1
					    SET @i = @I +1
					  end
					  IF OBJECT_ID('tempdb..#tmpBCE2') IS NOT NULL  DROP TABLE #tmpBCE2 
					 
					  SELECT @NurseCode = QXNurseCode FROM #tmpVAT1
					  select @Count = COUNT(1) from #tmpVAX1
					  select IDENTITY( INT,1,1 ) AS ID , BCE02,BCE03 into #tmpBCE3 from BCE1
					   where BCE02 in (select * from SPLIT(@NurseCode,'/'))
					  select @i = 0,@Count2 = count(*) from #tmpBCE3 
					  while @i<@Count
					  begin
					    insert into #tmpVAX1(ID , BCE02 , BCE03 , ACE01) 
					    select @Count + @i+1,BCE02, BCE03 , '23' FROM #tmpBCE3 WHERE ID = @i+1
					    SET @i = @I +1
					  end
					 IF OBJECT_ID('tempdb..#tmpBCE3') IS NOT NULL  DROP TABLE #tmpBCE3*/
			//创建临时表
			if(date1==""){
				mapList.remove("vat09");
				mapList.remove("vat10");
				mapList.put("vat09","1990-01-01");
				mapList.put("vat10","1990-01-01");
			}
			osHooperDao.createTmpVAX1(mapList);
			List listtmpVAX111= osHooperDao.selectTmpVAX1Count();
			int count= 1;
			int i = 0;
			mapList.put("i",i);
			mapList.put("count",count);
			while(i < count){
				mapList.remove("i");
				mapList.put("i",i);
				osHooperDao.insertTmpVAX1TmpBCE11(mapList);
				//xs
				osHooperDao.insertTmpVAX1TmpBCE1(mapList);
				//xh
				osHooperDao.insertTmpVAX1TmpBCE2(mapList);
				//qxnurse!=
				osHooperDao.insertTmpVAX1TmpBCE3(mapList);
				i++;
			}
			/*while(i < count){
				mapList.remove("i");
				mapList.put("i",i);
				i++;
			}
			while(i < count){
				mapList.remove("i");
				mapList.put("i",i);
				
				i++;
			}
			while(i < count){
				mapList.remove("i");
				mapList.put("i",i);
				
				i++;
			}*/
			osHooperDao.deleteVAX1(mapList);
			
			//更新病人手术信息
			List listtmpVAX1= osHooperDao.selectTmpVAX1Count();
			/*Map maptmpVAX1= (Map) listtmpVAX1.get(0);
			int i3=GlobalVariable.IntegerIsNull(maptmpVAX1.get("i"));
			int count1=GlobalVariable.IntegerIsNull(maptmpVAX1.get("count"));
			mapList.put("i3",i3);
			while(i3 < count1){
				mapList.remove("i3");
				mapList.put("i3",i3);
				String BCE02 = osHooperDao.selectBCE02(mapList);
				mapList.put("BCE02", BCE02);
				if(BCE02!=""&&BCE02!=null){
					String Value = osHooperDao.selectValue();
					int BCE01 = osHooperDao.selectBCE01(mapList);
					mapList.put("Value", Value);
					mapList.put("BCE01", BCE01);
					int i4=osHooperDao.insertVAX1(mapList);
					if(i4 <= 0){
						mv.setResult(StaticKeys.OH_FALSE);
						mv.setResultMsg("更新病人手术信息时发生错误！");
						return mv;
					}
					mapList.remove("Value");
					mapList.remove("BCE01");
					mapList.remove("BCE02");
				}
				i3++;
			}*/
			for (int  i1=0;  i1< listtmpVAX1.size(); i1++) {
				Map maptmpVAX1= (Map) listtmpVAX1.get(i1);
				String BCE02 = osHooperDao.selectBCE02(maptmpVAX1);
				mapList.put("BCE02", BCE02);
				if(BCE02!=""&&BCE02!=null){
					String Value = osHooperDao.selectValue();
					int BCE01 = osHooperDao.selectBCE01(mapList);
					mapList.put("Value", Value);
					mapList.put("BCE01", BCE01);
					mapList.put("id", maptmpVAX1.get("id"));
					int i4=osHooperDao.insertVAX1(mapList);
					if(i4 <= 0){
						mv.setResult(StaticKeys.OH_FALSE);
						mv.setResultMsg("更新病人手术信息时发生错误！");
						return mv;
					}
					mapList.remove("Value");
					mapList.remove("BCE01");
					mapList.remove("BCE02");
					mapList.remove("id");
				}
			}
				
			//************************************病人手术情况***********************************/
			String BBX05 = GlobalVariable.StringIsNull(mapList.get("bbx05"));
			mapList.put("valueID", 0);
			//拟行手术  hhh
			if(BBX05 !="" && BBX05 != null){
				int countVAV11 = osHooperDao.selectcountVAV11(mapList);
				String VAV011 = osHooperDao.selectVAV011(mapList);
				//--没有写入病人拟施手术
				if(countVAV11 == 0){
					//Core_NewIDEx
					int valueID=CoreNewIDEx("VAV1" , "VAV01" ,1,mv);
					try {
						mapList.remove("valueID");
						mapList.put("valueID", valueID);
						if(GlobalVariable.StringIsNull(mapList.get("bbx01"))==""){
							
						}else{
							osHooperDao.insertVAV1(mapList);
						}
					} catch (Exception ex) {
						ex.printStackTrace();
						throw new BaseException("插入病人手术情况时发生错误,请重试！");
					}	
				}else{
					try {
						mapList.put("VAV011", VAV011);
						if(GlobalVariable.StringIsNull(mapList.get("bbx01"))==""){
							
						}else{
							osHooperDao.updateVAV1(mapList);
						}
						
					} catch (Exception ex) {
						ex.printStackTrace();
						throw new BaseException("更新病人手术情况时发生错误,请重试！");
					}	
				}
			}else{
				mapList.put("vav04",0);
				osHooperDao.deleteVAV1(mapList);
			}
			
			//已施手术
			String YsBBX05 = GlobalVariable.StringIsNull(mapList.get("ysbbx05"));
			if(YsBBX05 !="" && YsBBX05 != null){
				int countVAV12 = osHooperDao.selectcountVAV12(mapList);
				String VAV012 = osHooperDao.selectVAV012(mapList);
				if(countVAV12 == 0){
					int valueID=CoreNewIDEx("VAV1" , "VAV01" ,1,mv);
					mapList.remove("valueID");
					mapList.put("valueID", valueID);
					if(GlobalVariable.StringIsNull(mapList.get("ysbbx01"))==""){
						
					}else{
						osHooperDao.insertVAV12(mapList);
					}
				}else{
					try {
						map.put("VAV012", VAV012);
						if(GlobalVariable.StringIsNull(mapList.get("ysbbx01"))==""){
							
						}else{
							osHooperDao.updateVAV12(mapList);
						}
					} catch (Exception ex) {
						ex.printStackTrace();
						throw new BaseException("更新病人手术情况时发生错误,请重试！");
					}	
				}
			}else{
				mapList.put("vav04",1);
				osHooperDao.deleteVAV1(mapList);
			}
		osHooperDao.deletetmpVAX1();
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	//--存储过程.Core_NewIDEx
	@SuppressWarnings("rawtypes")
	public int CoreNewIDEx(String TName, String CName, int Increment, ModelVo mv) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("TName", TName);
		map.put("CName", CName);
		map.put("Increment", Increment);
		int ID = osHooperDao.selectID(map);
		int value=0;
		if(String.valueOf(ID) == ""){
			int IDNEW = CoreNewID("Core_SequenceObjects", "ID",0,mv);
			map.put("IDNEW", IDNEW);
			osHooperDao.insertCoreSequenceObjects(map);
		}
		map.put("ID", ID);
		List CoreSequencesList = osHooperDao.selectCoreSequencesList(map);
		if(CoreSequencesList.size() > 0){
			try {
				osHooperDao.updateCoreSequences(map);	
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new BaseException("获取序号ID失败,请重试！");
			}	
			try {
				value=osHooperDao.selectCoreSequencesValue(map);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw new BaseException("获取序号ID失败,请重试！");
			}
		}else{
			value = Increment;
			osHooperDao.insertCoreSequencesValue(map);
		}
		
		return value;
	}

	//--存储过程.Core_NewID
	public int CoreNewID(String TName, String CName, int Recycle, ModelVo mv) {
		String sql="";
		int ID;
		if(Recycle == 1){
			sql = "select min(" + CName + ") from " + TName + " a"
				      + " where " + CName + " >= 1"
				      + " and not exists(select * from " + TName + " b where b."
				      + CName + " = a." + CName + " + 1)";
		}else{
			sql = "select max(" + CName + ") from " + TName;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sql", sql);
		try {
			ID = osHooperDao.selectValueID(map);
			if(String.valueOf(ID) == ""){
				ID = 1;
			}else{
				ID += 1;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new BaseException("获取新的ID失败.");
		}
		return ID;
	}

	public ModelVo updateHOOper833826(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try {
			//更新手术间
			if(map.get("BEE03")!=null){
				int i1 = osHooperDao.updateHOOper833(map);
				if(i1 <= 0){
					mv.setResult(StaticKeys.OH_FALSE);
					mv.setResultMsg("更新手术间时发生错误！");
					return mv;
				}
			}
			
			//更新手术记录手术状态标志
			int i2 = osHooperDao.updateHOOper826(map);
			if(i2 <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新手术记录手术状态标志时发生错误！");
				return mv;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo hOOper826(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try {
			//更新手术记录手术状态标志
			int i2 = osHooperDao.updateHOOper826(map);
			if(i2 <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新手术记录手术状态标志时发生错误！");
				return mv;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

	public ModelVo hOOper826Os(Map<String, Object> map) {
		ModelVo mv = new ModelVo();
		try {
			//更新手术记录手术状态标志
			int i2 = osHooperDao.updateHOOper826Os(map);
			if(i2 <= 0){
				mv.setResult(StaticKeys.OH_FALSE);
				mv.setResultMsg("更新手术记录手术状态标志时发生错误！");
				return mv;
			}
			if(GlobalVariable.IntegerIsNull(map.get("VAT04"))==3){
				osHooperDao.updateHOOper833Bee1(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw BaseException.getDetailMessage(e);
		}
		mv.setResult(StaticKeys.OH_SUCCESS);
		return mv;
	}

}
